//
//  DownloadedCell.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 12-12-11.
//  Copyright (c) 2012年 wzyk. All rights reserved.
//

#import "DownloadingCell.h"

@implementation DownloadingCell

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateProgress:) name:kUpdateProgressInShelfCell object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDisplay) name:kUpdateDownloadStatus object:nil];
    }
    return self;
}

- (void)updateDisplay
{
    NSString *item_name = [self.downloadInfo.infoDic valueForKey:@"item_name"];
    NSString *sub_name = [self.downloadInfo.infoDic valueForKey:@"sub_name"];
    titleLabel.text = [NSString stringWithFormat:@"%@ %@",item_name,sub_name];
    downloadProgressView.progress = self.downloadInfo.lastPercent;
    if ([[DownloadManager defaultManager] isDownloading:[self.downloadInfo.infoDic valueForKey:@"subitem_id"]]) {
//        NSLog(@"正在下载中");
        [downloadButton setBackgroundImage:[UIImage imageNamed:@"downloading_btn"] forState:UIControlStateNormal];
    }
    else {
//        NSLog(@"暂停状态中");
        [downloadButton setBackgroundImage:[UIImage imageNamed:@"download_icon"] forState:UIControlStateNormal];
    }
    if (self.chapterIndex%2 == 0) {
        self.contentView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
    }
    else {
        self.contentView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.1];
    }
}

- (IBAction)downloadAction:(id)sender
{
    if ([[DownloadManager defaultManager] isDownloading:[self.downloadInfo.infoDic valueForKey:@"subitem_id"]]) {
        NSLog(@"将暂停下载");
        [[DownloadManager defaultManager] cancelDownload];
    }
    else {
        NSLog(@"将新建下载");
        [[DownloadManager defaultManager] cancelDownload];
        [[DownloadManager defaultManager] redownloadWithID:[self.downloadInfo.infoDic valueForKey:@"subitem_id"]];
    }
    [self updateDisplay];
}

- (void)updateProgress:(NSNotification *)notification
{
    downloadProgressView.progress = self.downloadInfo.lastPercent;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    if (selected) {
        if (self.editing) {
            if (multiSelected) {
                checkImageView.image = [UIImage imageNamed:@"checkbox"];
                [_tasksToDeleteArray removeObject:[_downloadInfo.infoDic objectForKey:@"subitem_id"]];
            }
            else {
                checkImageView.image = [UIImage imageNamed:@"checkbox_selected"];
                [_tasksToDeleteArray addObject:[_downloadInfo.infoDic objectForKey:@"subitem_id"]];
            }
            multiSelected = !multiSelected;
        }
    }
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    if (editing) {
        [UIView animateWithDuration:animated ? 0.5 : 0 animations:^{
            CGRect rect = containerView.frame;
            rect.origin.x = 0;
            containerView.frame = rect;
            
            rect = titleLabel.frame;
            rect.size.width = 268;
            titleLabel.frame = rect;
            
            downloadProgressView.alpha = 0;
            downloadButton.alpha = 0;
        }];
        
        if ([_tasksToDeleteArray containsObject:[_downloadInfo.infoDic objectForKey:@"subitem_id"]]) {
            checkImageView.image = [UIImage imageNamed:@"checkbox_selected"];
            multiSelected = YES;
        }
        else {
            checkImageView.image = [UIImage imageNamed:@"checkbox"];
            multiSelected = NO;
        }
    }
    else {
        [UIView animateWithDuration:animated ? 0.5 : 0 animations:^{
            CGRect rect = containerView.frame;
            rect.origin.x = -24;
            containerView.frame = rect;
            
            
            
            downloadProgressView.alpha = 1;
            downloadButton.alpha = 1;
        }];
        
        [CATransaction begin];
        CATransition *animation = [CATransition animation];
        animation.duration = .5;
        [animation setFillMode:kCAFillModeForwards];
        [animation setType:kCATransitionFade];
        
        CGRect rect = titleLabel.frame;
        rect.size.width = 201;
        titleLabel.frame = rect;
        
        [titleLabel.layer addAnimation:animation forKey:@"titleLabel"];
        [CATransaction commit];

        
        multiSelected = NO;
    }
}

- (void)prepareForReuse
{
    multiSelected = NO;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [titleLabel release];
    [downloadProgressView release];
    [downloadButton release];
    [_downloadInfo release];
    [containerView release];
    [checkImageView release];
    [super dealloc];
}
@end
